package com.hry.store.service.impl;

import com.hry.store.dao.HotProductVoMapper;
import com.hry.store.dao.ProductVoMapper;
import com.hry.store.service.ProductVoService;
import com.hry.store.vo.HotProductVo;
import com.hry.store.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductVoServiceImpl implements ProductVoService {
    @Autowired
    private ProductVoMapper productVoMapper;

    @Autowired
    private HotProductVoMapper hotProductVoMapper;

    /**
     * 根据产品id查询产品
     *
     * @param secondCategoryId
     * @return
     */
    @Override
    public List<ProductVo> getBySecondCategoryId(Integer secondCategoryId) {

        List<ProductVo> productVoList = productVoMapper.getBySecondCategoryId(secondCategoryId);

        return productVo(productVoList);
    }

    /**
     * 查询家用电器商品
     * @return
     */
    @Override
    public List<ProductVo> getByTopCategoryId() {
        List<ProductVo> productVoList = productVoMapper.getByTopCategoryId();
        return productVo(productVoList);

    }

    /**
     * 首页轮播图产品信息
     * @return
     */
    @Override
    public List<HotProductVo> getHotProduct() {
        List<HotProductVo> productVoList = hotProductVoMapper.getHotProduct();
        List list = new ArrayList();
        for (HotProductVo productVos : productVoList) {
            HotProductVo productVo = new HotProductVo();
            productVo.setColorCode(productVos.getColorCode());
            productVo.setColorId(productVos.getColorId());
            productVo.setColorName(productVos.getColorName());
            productVo.setCreateTime(productVos.getCreateTime());
            productVo.setIsHotProduct(productVos.getIsHotProduct());
            productVo.setLunboImage(productVos.getLunboImage());
            productVo.setProductDesc(productVos.getProductDesc());
            productVo.setQuantity(productVos.getQuantity());
            productVo.setSecondCategoryId(productVos.getSecondCategoryId());
            productVo.setSecondCategoryName(productVos.getSecondCategoryName());
            productVo.setStatus(productVos.getStatus());
            productVo.setVersionPrice(productVos.getVersionPrice());
            list.add(productVo);
        }
        return list;

    }

    /**
     *鼠标悬停左面展示
     * @param topCategory
     * @return
     */
    @Override
    public List<ProductVo> getTopCategoryId(Integer topCategory) {
        List<ProductVo> productVoList = productVoMapper.getTopCategoryId(topCategory);
        return productVo(productVoList);
    }

    /**
     * 鼠标悬停右面展示
     * @param topCategory
     * @return
     */
    @Override
    public List<ProductVo> getCenterProduct(Integer topCategory) {
        List<ProductVo> productVoList = productVoMapper.getCenterProduct(topCategory);
        return productVo(productVoList);
    }

    // 商品封装
    private List productVo(List<ProductVo> product) {
        List list = new ArrayList();
        for (ProductVo productVos : product) {
            ProductVo productVo = new ProductVo();
            productVo.setColorCode(productVos.getColorCode());
            productVo.setColorId(productVos.getColorId());
            productVo.setColorName(productVos.getColorName());
            productVo.setCreateTime(productVos.getCreateTime());
            productVo.setIsHotProduct(productVos.getIsHotProduct());
            productVo.setIsStartProduct(productVos.getIsStartProduct());
            productVo.setProductDesc(productVos.getProductDesc());
            productVo.setQuantity(productVos.getQuantity());
            productVo.setSecondCategoryId(productVos.getSecondCategoryId());
            productVo.setSecondCategoryName(productVos.getSecondCategoryName());
            productVo.setStatus(productVos.getStatus());
            productVo.setVersionPrice(productVos.getVersionPrice());
            String imgs = productVos.getProductImgs();
            String[] img = imgs.split(",");
            productVo.setProductImgs(img[0]);
            list.add(productVo);
        }
        return list;
    }
}
